#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QRegularExpression>
#include <QClipboard>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 设置窗口标题
    setWindowTitle("TKLer");

    // statusbar添加label显示程序版本
    QLabel *lver = new QLabel(this);
    lver->setText("Version 3.0");
    ui->statusBar->addWidget(lver);

    // listwidget 连接 stackedwidget
    connect(ui->listWidget, &QListWidget::currentRowChanged, ui->stackedWidget, &QStackedWidget::setCurrentIndex);

    // page - 单位换算：2个数据重置按钮
    connect(ui->pushButton_10, &QPushButton::clicked, this, &MainWindow::on_pushButton_10_clicked);
    connect(ui->pushButton_11, &QPushButton::clicked, this, &MainWindow::on_pushButton_11_clicked);

    // page - 单位换算：正则限制只能输入数字
    QRegularExpression regExp("^-?\\d*\\.?\\d+$");
    QValidator *validator = new QRegularExpressionValidator(regExp, this);
    ui->lineEdit->setValidator(validator);
    ui->lineEdit_2->setValidator(validator);
    ui->lineEdit_3->setValidator(validator);
    ui->lineEdit_4->setValidator(validator);
    ui->lineEdit_5->setValidator(validator);
    ui->lineEdit_6->setValidator(validator);
    ui->lineEdit_7->setValidator(validator);
    ui->lineEdit_8->setValidator(validator);

    // page - 单位换算：长度转换
    connect(ui->lineEdit, &QLineEdit::textChanged, this, &MainWindow::on_lineEdit_textChanged);
    connect(ui->lineEdit_2, &QLineEdit::textChanged, this, &MainWindow::on_lineEdit_2_textChanged);
    connect(ui->lineEdit_3, &QLineEdit::textChanged, this, &MainWindow::on_lineEdit_2_textChanged);
    connect(ui->lineEdit_4, &QLineEdit::textChanged, this, &MainWindow::on_lineEdit_2_textChanged);

    // page - 单位换算：重量转换
    connect(ui->lineEdit_5, &QLineEdit::textChanged, this, &MainWindow::on_lineEdit_2_textChanged);
    connect(ui->lineEdit_6, &QLineEdit::textChanged, this, &MainWindow::on_lineEdit_2_textChanged);
    connect(ui->lineEdit_7, &QLineEdit::textChanged, this, &MainWindow::on_lineEdit_2_textChanged);
    connect(ui->lineEdit_8, &QLineEdit::textChanged, this, &MainWindow::on_lineEdit_2_textChanged);

    // page_2 - 英文大小写转换：垂直滑块控制 plaintextedit 中字体大小
    ui->verticalSlider->setRange(5, 40);
    ui->verticalSlider->setValue(20);

    ui->verticalSlider_3->setRange(5, 40);
    ui->verticalSlider_3->setValue(20);

    connect(ui->verticalSlider, &QSlider::valueChanged, ui->plainTextEdit, [=](int value){
        QFont font = ui->plainTextEdit->font();
        font.setPointSize(value);
        ui->plainTextEdit->setFont(font);
        });

    connect(ui->verticalSlider_3, &QSlider::valueChanged, ui->plainTextEdit_2, [=](int value1){
        QFont font1 = ui->plainTextEdit_2->font();
        font1.setPointSize(value1);
        ui->plainTextEdit_2->setFont(font1);
        });

    // page_2 - 英文大小写转换：首字母大写、全部大写、全部小写、清空输入
    connect(ui->pushButton_4, &QPushButton::clicked, this, &MainWindow::on_pushButton_4_clicked);
    connect(ui->pushButton_3, &QPushButton::clicked, this, &MainWindow::on_pushButton_3_clicked);
    connect(ui->pushButton_2, &QPushButton::clicked, this, &MainWindow::on_pushButton_2_clicked);
    connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::on_pushButton_clicked);
    connect(ui->pushButton_9, &QPushButton::clicked, this, &MainWindow::on_pushButton_9_clicked);

}

MainWindow::~MainWindow()
{
    delete ui;
}

// page - 单位换算：2个数据重置按钮-------------------------------------------------------------------
void MainWindow::on_pushButton_10_clicked()
{
    ui->lineEdit->clear();
    ui->lineEdit_2->clear();
    ui->lineEdit_3->clear();
    ui->lineEdit_4->clear();
}


void MainWindow::on_pushButton_11_clicked()
{
    ui->lineEdit_5->clear();
    ui->lineEdit_6->clear();
    ui->lineEdit_7->clear();
    ui->lineEdit_8->clear();
}

// page - 单位换算：长度转换 -------------------------------------------------------------------------
void MainWindow::on_lineEdit_textChanged(const QString &arg1)
{
    if(!ui->lineEdit->hasFocus())
    {
        return;
    }
    double icm = ui->lineEdit->text().toDouble();
    ui->lineEdit_2->setText(QString::number(icm * 0.39370079));
    ui->lineEdit_3->setText(QString::number(icm * 0.0328084));
    ui->lineEdit_4->setText(QString::number(icm * 0.01));
}


void MainWindow::on_lineEdit_2_textChanged(const QString &arg1)
{
    if(!ui->lineEdit_2->hasFocus())
    {
        return;
    }
    double iin = ui->lineEdit_2->text().toDouble();
    ui->lineEdit->setText(QString::number(iin * 2.54));
    ui->lineEdit_3->setText(QString::number(iin * 0.08333333));
    ui->lineEdit_4->setText(QString::number(iin * 0.0254));
}


void MainWindow::on_lineEdit_3_textChanged(const QString &arg1)
{
    if(!ui->lineEdit_3->hasFocus())
    {
        return;
    }
    double ift = ui->lineEdit_3->text().toDouble();
    ui->lineEdit->setText(QString::number(ift * 30.48));
    ui->lineEdit_2->setText(QString::number(ift * 12));
    ui->lineEdit_4->setText(QString::number(ift * 0.3048));
}


void MainWindow::on_lineEdit_4_textChanged(const QString &arg1)
{
    if(!ui->lineEdit_4->hasFocus())
    {
        return;
    }
    double im = ui->lineEdit_4->text().toDouble();
    ui->lineEdit->setText(QString::number(im * 100));
    ui->lineEdit_2->setText(QString::number(im * 39.3700787));
    ui->lineEdit_3->setText(QString::number(im * 3.2808399));
}

// page - 单位换算：重量转换 -------------------------------------------------------------------------

void MainWindow::on_lineEdit_5_textChanged(const QString &arg1)
{
    if(!ui->lineEdit_5->hasFocus())
    {
        return;
    }
    double gg = ui->lineEdit_5->text().toDouble();
    ui->lineEdit_6->setText(QString::number(gg * 0.00220462262185));
    ui->lineEdit_7->setText(QString::number(gg * 0.035274));
    ui->lineEdit_8->setText(QString::number(gg * 0.001));
}


void MainWindow::on_lineEdit_6_textChanged(const QString &arg1)
{
    if(!ui->lineEdit_6->hasFocus())
    {
        return;
    }
    double lb = ui->lineEdit_6->text().toDouble();
    ui->lineEdit_5->setText(QString::number(lb * 453.59237));
    ui->lineEdit_7->setText(QString::number(lb * 16));
    ui->lineEdit_8->setText(QString::number(lb * 0.4535924));
}


void MainWindow::on_lineEdit_7_textChanged(const QString &arg1)
{
    if(!ui->lineEdit_7->hasFocus())
    {
        return;
    }
    double oz = ui->lineEdit_7->text().toDouble();
    ui->lineEdit_5->setText(QString::number(oz * 28.3495231));
    ui->lineEdit_6->setText(QString::number(oz * 0.0625));
    ui->lineEdit_8->setText(QString::number(oz * 0.0283495));
}


void MainWindow::on_lineEdit_8_textChanged(const QString &arg1)
{
    if(!ui->lineEdit_8->hasFocus())
    {
        return;
    }
    double kg = ui->lineEdit_8->text().toDouble();
    ui->lineEdit_5->setText(QString::number(kg * 1000));
    ui->lineEdit_6->setText(QString::number(kg * 2.2046226));
    ui->lineEdit_7->setText(QString::number(kg * 35.2739619));
}

// page_2 - 英文大小写转换 -------------------------------------------------------------------------

void MainWindow::on_pushButton_4_clicked()
{
    QString engText = ui->plainTextEdit->toPlainText().trimmed();
    const int ll = engText.length();
    for (int i=0; i<ll; i++)
    {
        if (engText[i] == QChar(' ') || engText[i] == QChar('\t') || engText[i] == QChar('\n'))
        {
            engText[i+1] = engText[i+1].toUpper();
        }

        else if (i==0)
        {
            engText[0] = engText[0].toUpper();
        }
    }

    ui->plainTextEdit_2->setPlainText(engText.trimmed());
}


void MainWindow::on_pushButton_3_clicked()
{
    QString engText1 = ui->plainTextEdit->toPlainText();
    QString engText11 = engText1.toUpper();
    ui->plainTextEdit_2->setPlainText(engText11.trimmed());
}


void MainWindow::on_pushButton_2_clicked()
{
    QString engText2 = ui->plainTextEdit->toPlainText();
    QString engText22 = engText2.toLower();
    ui->plainTextEdit_2->setPlainText(engText22.trimmed());
}


void MainWindow::on_pushButton_clicked()
{
    ui->plainTextEdit->clear();
}


void MainWindow::on_plainTextEdit_textChanged()
{
    QString textPt = ui->plainTextEdit->toPlainText();
    int textPtNum = textPt.length();
    ui->label_11->setText(QString("%1").arg(textPtNum));
}


void MainWindow::on_plainTextEdit_2_textChanged()
{
    QString textPt1 = ui->plainTextEdit_2->toPlainText();
    int textPtNum1 = textPt1.length();
    ui->label_26->setText(QString("%1").arg(textPtNum1));
}


void MainWindow::on_pushButton_9_clicked()
{
    QString pTtext = ui->plainTextEdit_2->toPlainText().trimmed();
    QClipboard *copyPtText = QApplication::clipboard();
    copyPtText->setText(pTtext);
}

